package com.miku.blog.handler.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.miku.blog.domain.Article;
import com.miku.blog.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;


/**
 * @author shkstart
 * @create ${}YEAR-05-29 21:13
 */
@Component
public class MetaObjectHandlerImpl implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (Article.class.isAssignableFrom(metaObject.getOriginalObject().getClass())){
            Integer userId = SecurityUtils.getUserAuth().getId();
            this.setFieldValByName("userId",userId,metaObject);
        }
        Date date = new Date();
        this.setFieldValByName("createTime",date,metaObject)
                .setFieldValByName("updateTime",date,metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {

        Date date = new Date();
        this.setFieldValByName("updateTime",date,metaObject);
    }
}
